Fix TypeIs narrowing for covariant generic Any - #21662
Conversation
This comment has been minimized.
This comment has been minimized.
|
This doesn't address why this works for contravariant typevars. Please don't duplicate code. |
|
Updated, thanks. I removed the separate variance helper and route the TypeIs false-branch coverage check back through the existing type-parameter path instead. The new flag only changes variant parameter comparisons in this coverage check: invariant parameters stay exact/proper, while covariant and contravariant parameters use the existing declared-variance subtype logic. I also added the contravariant sibling regression case next to the covariant one, so the behavior that already worked stays covered. |
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
|
As per our policy we don't accept LLM generated PRs from new contributors. |
Fixes #21641.
Summary
TypeIschecks according to declared variance.TypeIs[Source[object]]narrow awaySource[Any]whenSourceis covariant.Box[Any]is not removed byTypeIs[Box[str]].Tests
python runtests.py check-typeispython runtests.py check-typeguard check-narrowingpython -m pytest mypy/test/testsubtypes.py -qpython -m mypy --config-file mypy_self_check.ini mypy/subtypes.pypython -m ruff check mypy/subtypes.pypython -m black --check mypy/subtypes.pygit diff --checkPrepared with AI assistance.